package pers.baiban.sqltoword.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.support.DefaultConversionService;

import java.util.List;

/**
 * 注册转换器
 *
 * @author 强子初
 * @since 2023/11/2
 */
@Configuration
public class ConvertConfig {

    private static final DefaultConversionService service = new DefaultConversionService();

    @Bean
    public ConverterRegistry registry(List<Converter<?, ?>> converterList,
                                      List<ConverterFactory<?, ?>> converterFactoryList,
                                      List<GenericConverter> genericConverterList) {
        // 注册普通转化器
        for (Converter<?, ?> converter : converterList) {
            service.addConverter(converter);
        }

        // 注册工厂转换器
        for (ConverterFactory<?, ?> converterFactory : converterFactoryList) {
            service.addConverterFactory(converterFactory);
        }

        // 注册复杂转换器
        for (GenericConverter genericConverter : genericConverterList) {
            service.addConverter(genericConverter);
        }
        return service;
    }

    @Bean
    public ConversionService service() {
        return service;
    }
}